package com.lysj.bhp.wallet.service;

import com.lysj.bhp.coinexchange.domain.Apply;
import com.lysj.bhp.investment.domain.Dividend;
import com.lysj.bhp.investment.domain.Reward;
import com.lysj.bhp.system.base.BaseConst;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.wallet.base.WalletContent;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import com.lysj.bhp.wallet.domain.BitCoinWallet;
import com.lysj.bhp.wallet.domain.EthereumWallet;
import com.lysj.bhp.wallet.vo.BhpcInfoVo;
import com.lysj.bhp.wallet.vo.WalletVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.web3j.crypto.WalletUtils;

import java.io.File;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class WalletService extends WalletContent {

    private static Logger logger = LoggerFactory.getLogger(Apply.class);

    //显示所有钱包的余额
    public Map<String, Object> showAllWalletBalance(Long userId) {

        BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(userId);
        EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(userId);
        BhpcWallet bhpcWallet = bhpcWalletRepository.findByUserId(userId);
        Map<String, Object> map = new HashMap<>();
        WalletVo bitcoinWallteVo = new WalletVo();
        bitcoinWallteVo.setTotalBalance(bitCoinWallet.getBalance());
        bitcoinWallteVo.setAvailBalance(bitCoinWallet.getAvailBalance());
        bitcoinWallteVo.setFreezeBalance(bitCoinWallet.getFreezeBalance());
        map.put("bitcoinWallteVo", bitcoinWallteVo);
        WalletVo ethereumWalletVo = new WalletVo();
        ethereumWalletVo.setTotalBalance(ethereumWallet.getBalance());
        ethereumWalletVo.setAvailBalance(ethereumWallet.getAvailBalance());
        ethereumWalletVo.setFreezeBalance(ethereumWallet.getFreezeBalance());
        map.put("ethereumWalletVo", ethereumWalletVo);
        WalletVo bhpcWalletVo = new WalletVo();
        bitcoinWallteVo.setTotalBalance(bhpcWallet.getBalance());
        bitcoinWallteVo.setAvailBalance(bhpcWallet.getAvailBalance());
        bitcoinWallteVo.setFreezeBalance(bhpcWallet.getFreezeBalance());
        map.put("bhpcWalletVo", bhpcWalletVo);
        return map;
    }

//    public BhpcInfoVo showBhpcInfo(Long userId) {
//        User user = userRepository.findByUserId(userId);
//        Reward reward = rewardRepository.findByUserId(userId);
//        Dividend dividend=dividendRepository.findByUserId(userId);
//        BigDecimal btcToDollar = exchangeRateService.getBitcoinToDollar();
//        BigDecimal dollarToCnyRate = exchangeRateService.dollarToCnyRate();
//        BhpcInfoVo bhpcWaletVo = new BhpcInfoVo();
//        bhpcWaletVo.setBtcDividend(dividend.getDividend()); //利息BCT为单位
//        bhpcWaletVo.setReward(reward.getBalance()); //奖励BHPC
//        bhpcWaletVo.setCnyDividend(((dividend.getDividend()).divide(btcToDollar)).multiply(dollarToCnyRate));//利息CNY为单位
//        bhpcWaletVo.setLastDividend(((dividendController.lastDividend(new Date(), userId)).divide(btcToDollar)).multiply(dollarToCnyRate));//昨日利息CNY
//        return bhpcWaletVo;
//    }



    public void myBhpcWallet() throws Exception {

        String password = ParamUtil.uuid();
        String keystoreFilename = WalletUtils.generateFullNewWalletFile(password, new File(BaseConst.KEY_STORE_DIR));

        String[] addressArray = keystoreFilename.split("--");
        String address = addressArray[2].substring(0, 40);

        BhpcWallet bhpcWallet = new BhpcWallet();
        bhpcWallet.setKeystore(keystoreFilename);
        bhpcWallet.setPassword(password);
        bhpcWallet.setUserId(currentUser().getUserId());
        bhpcWallet.setAddress(address);
        bhpcWallet.setBalance(BigDecimal.ZERO);
        bhpcWallet.setFreezeBalance(BigDecimal.ZERO);
        bhpcWallet.setAvailBalance(BigDecimal.ZERO);
        bhpcWallet.setBookKeepBalance(BigDecimal.ZERO);
        logger.info(bhpcWallet.toString());
        bhpcWalletRepository.save(bhpcWallet);
    }


}
